﻿

#include "zyTrackBar.h"

BlackMoonUI::zyTrackBar::zyTrackBar()
{
	SetMember(L"this", (LONG_PTR)this);
	m_obj->setWndProc(zyCommand_WndProc);
	m_obj->setChildWndProc(zyTrackBar_ChildWndProc);
	m_obj->setDestroy(zyCommand_Destroy);
	setType(zyType_Trackbar);
}
BlackMoonUI::zyTrackBar::~zyTrackBar()
{

}
void BlackMoonUI::zyTrackBar::OnNCDestroy()
{
	HGDIOBJ BackBrush = (HGDIOBJ)GetMember(L"BackBrush");
	if (BackBrush != 0) {
		DeleteObject(BackBrush);
		SetMember(L"BackBrush", 0);
	}
}
void WINAPI BlackMoonUI::zyTrackBar::zyCommand_Destroy(zyObject* obj)
{
	zyTrackBar* zyc = (zyTrackBar*)obj->getPtrData(L"this");
	zyc->OnNCDestroy();
	zyCtrol::zyCtrol_Destroy(obj);
}
LRESULT WINAPI BlackMoonUI::zyTrackBar::zyCommand_WndProc(zyObject* obj, HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL* bHandled)
{
	if (uMsg == WM_NCDESTROY) {
		zyTrackBar* zyc = (zyTrackBar*)obj->getPtrData(L"this");
		zyc->OnNCDestroy();
	}
	return zyCtrol::zyCtrol_WndProc(obj, hWnd, uMsg, wParam, lParam, bHandled);
}
LRESULT WINAPI BlackMoonUI::zyTrackBar::zyTrackBar_ChildWndProc(zyObject* obj, HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam, BOOL* bHandle)
{
	if (msg == WM_HSCROLL || msg == WM_VSCROLL) {
		TRegEvent* evtPtr = (TRegEvent*)obj->getRegEvent(L"OnPosChange");
		if (evtPtr != nullptr) {
			int nScrollCode = LOWORD(wParam);
			int nPos = 0;
			if (nScrollCode == TB_THUMBTRACK || nScrollCode == TB_THUMBPOSITION)
				nPos = HIWORD(wParam);
			else
				nPos = (int)SendMessage((HWND)lParam, TBM_GETPOS, 0, 0);
			evtPtr->handler<int>(lParam, nPos);
		}
		else if (msg >= WM_CTLCOLORMSGBOX && msg <= WM_CTLCOLORSTATIC) {
			LRESULT BackBrush = (LRESULT)obj->getPtrData(L"BackBrush");
			if (BackBrush != 0) {
				*bHandle = true;
				SetBkColor((HDC)wParam, (COLORREF)(INT_PTR)obj->getPtrData(L"BackColor"));
				return BackBrush;
			}
		}
	}
	return zyCtrol::zyCtrol_ChildWndProc(obj, hWnd, msg, wParam, lParam, bHandle);
}

BOOL BlackMoonUI::zyTrackBar::创建(HWND 参数_父窗口, const std::wstring& 参数_标题, POINT 参数_位置, POINT 参数_尺寸, int 参数_ID, int 参数_窗口风格, int 参数_扩展风格)
{
	if (参数_窗口风格 == 0)
		参数_窗口风格 = TBS_AUTOTICKS | TBS_TOOLTIPS;
	if (参数_ID == NULL)
		参数_ID = 黑月默认ID();

	return _CreateCtrol(参数_父窗口, 参数_标题, 参数_位置, 参数_尺寸, 参数_ID, 参数_窗口风格 , 参数_扩展风格, L"msctls_trackbar32");
}

BOOL BlackMoonUI::zyTrackBar::创建(zyWindow& 参数_父窗口, const std::wstring& 参数_标题, POINT 参数_位置, POINT 参数_尺寸, int 参数_ID, int 参数_窗口风格, int 参数_扩展风格)
{
	if (参数_窗口风格 == 0)
		参数_窗口风格 = TBS_AUTOTICKS | TBS_TOOLTIPS;
	if (参数_ID == NULL)
		参数_ID = 黑月默认ID();

	return _CreateCtrol(参数_父窗口.getHwnd(), 参数_标题, 参数_位置, 参数_尺寸, 参数_ID, 参数_窗口风格 , 参数_扩展风格, L"msctls_trackbar32");

}
int BlackMoonUI::zyTrackBar::背景色(int 参数_背景色)
{
	if (参数_背景色 == 0) {
		return (int)(INT_PTR)GetMember(L"BackColor");
	}
	else {
		HBRUSH hBrush = (HBRUSH)GetMember(L"BackBrush");
		if (hBrush != 0)
			DeleteObject((HGDIOBJ)hBrush);
		hBrush = CreateSolidBrush((COLORREF)参数_背景色);
		SetMember(L"BackBrush", (LONG_PTR)hBrush);
		SetMember(L"BackColor", 参数_背景色);
		return 0;
	}
}
int BlackMoonUI::zyTrackBar::最小位置(int 参数_最小位置) {
	if (参数_最小位置 == NULL)
		return (int)sendMsg(TBM_GETRANGEMIN, 0, 0);
	else
		return (int)sendMsg(TBM_SETRANGEMIN, 1, 参数_最小位置);
}
int BlackMoonUI::zyTrackBar::位置(int 参数_位置) {
	if (参数_位置 == -1)
		return (int)sendMsg(TBM_GETPOS, 0, 0);
	else
		return (int)sendMsg(TBM_SETPOS, 1, 参数_位置);
}
int BlackMoonUI::zyTrackBar::最大位置(int 参数_最大位置) {
	if (参数_最大位置 == NULL)
		return (int)sendMsg(TBM_GETRANGEMAX, 0, 0);
	else
		return (int)sendMsg(TBM_SETRANGEMAX, 1, 参数_最大位置);
}
int BlackMoonUI::zyTrackBar::首选择位置(int 参数_首选择位置) {
	if (参数_首选择位置 == NULL)
		return (int)sendMsg(TBM_GETSELSTART, 0, 0);
	else
		return (int)sendMsg(TBM_SETSELSTART, 1, 参数_首选择位置);
}
int BlackMoonUI::zyTrackBar::选择长度(int 参数_选择长度) {
	if (参数_选择长度 == NULL)
		return (int)sendMsg(TBM_GETSELEND, 0, 0);
	else
		return (int)sendMsg(TBM_SETSELEND, 1, 参数_选择长度);
}
int BlackMoonUI::zyTrackBar::滑块跨度(int 参数_滑块跨度) {
	if (参数_滑块跨度 == NULL)
		return (int)sendMsg(TBM_GETTIC, 0, 0);
	else
		return (int)sendMsg(TBM_SETTIC, 1, 参数_滑块跨度);
}
void BlackMoonUI::zyTrackBar::置刻度单位值(int 参数_刻度单位值) {
	sendMsg(TBM_SETTICFREQ, 参数_刻度单位值, 0);
}
int BlackMoonUI::zyTrackBar::行改变值(int 参数_行改变值) {
	if (参数_行改变值 == NULL)
		return (int)sendMsg(TBM_GETLINESIZE, 0, 0);
	else
		return (int)sendMsg(TBM_SETLINESIZE, 0, 参数_行改变值);
}
int BlackMoonUI::zyTrackBar::页改变值(int 参数_页改变值) {
	if (参数_页改变值 == NULL)
		return (int)sendMsg(TBM_GETPAGESIZE, 0, 0);
	else
		return (int)sendMsg(TBM_SETPAGESIZE, 0, 参数_页改变值);
}